#include <bits/stdc++.h>
using namespace std;
#define int long long
#define re register
#define lowbit(x) (x & (-x))
typedef pair<int, int> PII;
const int INF = 1e18;
const int N = 1e5 + 10;
vector<int> G[N];
vector<int> ans(N);
int l = INF;
void dfs(int u, int fa)
{
    for (auto v : G[u])
    {
        if (v == fa)
            continue;
        dfs(v, u);
        ans[u] += ans[v] + abs(v - u);
    }
}

// 5 4 5 1 2 1 3 1 4
void dfs1(int u, int fa)
{
    for (auto v : G[u])
    {
        if (v == fa)
            continue;
        l = min(l, abs(ans[v] - (ans[1] - ans[v] - abs(u - v))));
        dfs1(v, u);
    }
}
void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++) cout << ans[i] << " ";
    cout << endl;
    dfs1(1, 0);
    cout << l << '\n';
}
int T = 1;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // cin >> T;
    while (T--)
        solve();
    return 0;
}
